﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Lhn.Ds.Core.Controll
{
    public class BaseUserControll : ContentControl, IControl
    {
        public const string PartMaxButton = "PART_MaxButton";
        public const string PartMinButton = "PART_MinButton";
        public const string PartCloseButton = "PART_CloseButton";
        public const string PartTitle = "PART_Title";

        #region 窗体风格属性

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
            nameof(Title), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(string)));
        /// <summary>
        /// 组件的名称，就是窗体名称
        /// </summary>
        public string Title
        {
            get => (string) GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public static readonly DependencyProperty TitleOfBarProperty = DependencyProperty.Register(
            nameof(TitleOfBar), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(string)));
        /// <summary>
        /// 组件在工具栏的名称
        /// </summary>
        public string TitleOfBar
        {
            get => (string) GetValue(TitleOfBarProperty);
            set => SetValue(TitleOfBarProperty, value);
        }

        

        public static readonly DependencyProperty ShowBorderProperty = DependencyProperty.Register(
            nameof(ShowBorder), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(bool)));

        /// <summary>
        /// 是否显示边框、无边框包括无状态栏使用这个属性为false即可
        /// </summary>
        public bool ShowBorder
        {
            get => (bool) GetValue(ShowBorderProperty);
            set => SetValue(ShowBorderProperty, value);
        }

        public static readonly DependencyProperty ShowMinButtonProperty = DependencyProperty.Register(
            nameof(ShowMinButton), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(bool)));

        /// <summary>
        /// 显示最小化按钮
        /// </summary>
        public bool ShowMinButton
        {
            get => (bool) GetValue(ShowMinButtonProperty);
            set => SetValue(ShowMinButtonProperty, value);
        }

        public static readonly DependencyProperty ShowMaxButtonProperty = DependencyProperty.Register(
            nameof(ShowMaxButton), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(bool)));

        /// <summary>
        /// 显示最大化按钮
        /// </summary>
        public bool ShowMaxButton
        {
            get => (bool) GetValue(ShowMaxButtonProperty);
            set => SetValue(ShowMaxButtonProperty, value);
        }

        public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.Register(
            nameof(ShowCloseButton), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(bool)));

        /// <summary>
        /// 显示关闭按钮
        /// </summary>
        public bool ShowCloseButton
        {
            get => (bool) GetValue(ShowCloseButtonProperty);
            set => SetValue(ShowCloseButtonProperty, value);
        }

        public static readonly DependencyProperty ShowStatusBarProperty = DependencyProperty.Register(
            nameof(ShowStatusBar), typeof(bool), typeof(BaseUserControll), new PropertyMetadata(default(bool)));

        /// <summary>
        /// 显示状态栏
        /// </summary>
        public bool ShowStatusBar
        {
            get => (bool) GetValue(ShowStatusBarProperty);
            set => SetValue(ShowStatusBarProperty, value);
        }

        #endregion

        #region 窗体组件初始化

        public Button MaxButton { get; private set; }
        public Button MinButton { get; private set; }
        public Button CloseButton { get; private set; }
        public TextBlock TitleElement { get; private set; }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            //获取组件
            MaxButton = GetTemplateChild(PartMaxButton) as Button;
            MinButton = GetTemplateChild(PartMinButton) as Button;
            CloseButton = GetTemplateChild(PartCloseButton) as Button;
            TitleElement = GetTemplateChild(PartTitle) as TextBlock;
            //绑定事件

        }


        static BaseUserControll()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseUserControll),
                new FrameworkPropertyMetadata(typeof(BaseUserControll)));
        }

        #endregion


        #region 给容器注册的回调

        /// <summary>
        /// 给容器的回调，容器会在初始化组件的时候监听这个回调
        /// </summary>
        public Action<BaseUserControll> OnCloseCall { get; set; }

        #endregion

        #region 组件生命周期事件

        public bool IsSuspended { get; private set; }

        public BaseUserControll()
        {
        }

        public virtual void OnActive(ControllEvent e)
        {
            IsSuspended = false;
        }

        public virtual void OnClosing(ControllEvent e)
        {
        }

        public virtual void OnClosed(ControllEvent e)
        {
            IsSuspended = true;
        }

        public virtual void OnSuspending(ControllEvent e)
        {
        }

        public virtual void OnSuspended(ControllEvent e)
        {
            IsSuspended = true;
        }

        public virtual void OnAction(string type, object param)
        {
        }

        public virtual void Init(object param)
        {
        }

        #endregion

        #region 组件的方法

        /// <summary>
        /// 本地调用关闭方法，不会处理OnClose的handle
        /// </summary>
        public void Close()
        {
            OnCloseCall?.Invoke(this);
        }

        #endregion
    }
}